using System;

namespace Profit.ViewModels
{
    using Core.Contracts.Interfaces;
    using Services;
    using Views.Interfaces;
    using Core;
using System.Windows.Input;
    using Microsoft.Win32;
    using System.Windows;
    using Profit.Views;

    public sealed class AgentViewModel : ViewModelBase<IAgentView>
    {
        private IAgent _selectedAgent;
        private ICommand _importCommand;
        private ICommand _selectPortraitCommand;

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        public AgentViewModel(IAgentView view) 
            : base(view)
        {
        }

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        /// <param name="agent"></param>
        public AgentViewModel(IAgentView view, IAgent agent)
            : base(view)
        {
            _selectedAgent = agent;
        }

        public IAgent SelectedAgent
        {
            get { return _selectedAgent; } 
            set
            {
                _selectedAgent = value;
                NotifyPropertyChange(()=>SelectedAgent);
            }
        }

        public ICommand ImportExtensionsCommand
        {
            get { return _importCommand ?? (_importCommand = new BindableCommand<object>(ImportExtensionsCommandHandler)); }
        }

        public ICommand SelectPortraitCommand
        {
            get {  return _selectPortraitCommand ?? (_selectPortraitCommand = new BindableCommand<object>(SelectPortraitCommandHandler)); }
        }

        private void ImportExtensionsCommandHandler(object state)
        {
            if(_selectedAgent == null) return;

            var ofd = new OpenFileDialog()
                          {
                              Filter = "csv|*.csv|all|*.*",
                              Multiselect = false
                          };

            var result = ofd.ShowDialog(this.View as Window);
            if(result.HasValue && result.Value)
            {
                try
                {
                    Helpers.ParseExtensionHistoryFile(ofd.FileName,_selectedAgent);
                }
                catch (Exception exp)
                {
                    var vm = new ConfirmationViewModel(new ConfirmationView())
                                 {
                                     Title = "Error",
                                     ConfirmationMessage = string.Format(
                                             "An exception of type \"{0}\" occured while attempting to import extensions.\r\n\r\nDetails: {1}",
                                             exp.GetType(), exp.Message),
                                     ShowButtonCancel = false,
                                     ButtonOkContent = "Ok"
                                 };

                    vm.View.ShowDialog(this.View);
                }
            }

        }

        private void SelectPortraitCommandHandler(object value)
        {
            if(_selectedAgent == null) return;

            var vm = new PortraitViewModel(
                new PortraitView(),
                _selectedAgent.HasImageSource ? _selectedAgent.ImageSource.ToString() : null);

            var result = vm.View.ShowDialog(this.View);
            if (!result.HasValue || !result.Value) return;
            
            _selectedAgent.ImageSource = string.IsNullOrEmpty(vm.ImagePath) ? null : vm.ImageUri;
        }

        internal override void OnValidate(object value)
        {
            base.OnValidate(value);
            Repository.SaveAgents();
        }
    }
}